<?php

function datosMesa($datos) {
    $lines = explode("\n", $datos);
    $n_lines = count($lines);
    $window_width = 0;
    for ($i = 0; $i < $n_lines; $i++) {
        $window_width = max($window_width, strlen($lines[$i]));
    }
    $window_width += 4;

    $x_coords = 20;
    $y_coords = 0;
    $window = ncurses_newwin(2 + $n_lines, $window_width, $y_coords, $x_coords);
    ncurses_wborder($window, 0, 0, 0, 0, 0, 0, 0, 0);
    for ($j = 0; $j < $n_lines; $j++) {
        ncurses_mvwaddstr($window, 1 + $j, 1, $lines[$j]);
        ncurses_wrefresh($window);
        $panel = ncurses_new_panel($window);
    }

    ncurses_update_panels();
    ncurses_curs_set(0);
    ncurses_noecho();
}

function datosCarga($datos) {
    $categorias_generales = categorias_generales();

    $texto = "RENG   GOB  SEN  DIP  INT  C/M\n";
    $texto.= "------------------------------\n";
    foreach ($datos as $clave => $item) {
        $texto.=str_pad($clave, 2, " ", STR_PAD_LEFT) . ":   ";

        foreach ($categorias_generales as $valor) {
            if (isset($item[$valor])) {
                $texto.=str_pad($item[$valor], 4, " ", STR_PAD_LEFT) . " ";
            } else {
                if ($valor != 'Concejal') // Para que imprime Conecejal o MCO
                    $texto.=str_pad("   ", 4, " ", STR_PAD_LEFT) . " ";
            }
        }
        $texto.="\n";
    }
    // print_r($texto);die;
    $lines = explode("\n", $texto);
    $n_lines = count($lines);
    $window_width = 0;
    for ($i = 0; $i < $n_lines; $i++) {
        $window_width = max($window_width, strlen($lines[$i]));
    }
    $window_width += 2;
    $x_coords = 1;
    $y_coords = 6;
    $window = ncurses_newwin(2 + 14, $window_width, $y_coords, $x_coords);
    ncurses_wborder($window, 0, 0, 0, 0, 0, 0, 0, 0);
    for ($j = 0; $j < $n_lines; $j++) {
        ncurses_mvwaddstr($window, 1 + $j, 1, $lines[$j]);
        ncurses_wrefresh($window);
    }
    for ($i = $j; $i < 14; $i++) {
        ncurses_mvwaddstr($window, 1 + $i, 1, "   ");
        ncurses_wrefresh($window);
    }
    $window2 = ncurses_newwin(2 + 14, $window_width, $y_coords, $x_coords + 40);
    ncurses_wborder($window2, 0, 0, 0, 0, 0, 0, 0, 0);
    for ($j = 0; $j < 14; $j++) {
        ncurses_mvwaddstr($window2, 1 + $j, 1, "               ");
        ncurses_wrefresh($window2);
        //$panel = ncurses_new_panel($window);
    }
    //ncurses_update_panels();

    ncurses_curs_set(0);
    ncurses_noecho();
}

function lista($lista) {
    $windowlista = ncurses_newwin(1, 18, 4, 9);
    $texto = "         ";
    if ($lista != "")
        $texto = $lista;
    ncurses_mvwaddstr($windowlista, 0, 0, $texto);
    ncurses_wrefresh($windowlista);
}

function dlg_input($params = array()) {
    $title = isset($params['title']) ? $params['title'] : NULL;
    $max_length = isset($params['max_len']) ? (int) $params['max_len'] : 10;
    $dlg_rows = isset($params['dlg_rows']) ? (int) $params['dlg_rows'] : 3;
    $dlg_cols = isset($params['dlg_cols']) ? (int) $params['dlg_cols'] : 16;
    $parent_cols = isset($params['cols']) ? (int) $params['cols'] : NULL;
    $parent_rows = isset($params['rows']) ? (int) $params['rows'] : NULL;
    $valor = isset($params['valor']) ? (int) $params['valor'] : NULL;
    $clear = isset($params['clear']) ? (int) $params['clear'] : 1;
    $dlg_x = (int) (($parent_cols - $dlg_cols) / 2);
    if ($dlg_x < 0)
        $dlg_x = 0;
    $dlg_y = (int) (($parent_rows - $dlg_rows) / 2);
    if ($dlg_y < 0)
        $dlg_y = 0;

    if ($max_length <= 0 || $dlg_rows <= 0 || $dlg_cols <= 0) {
        trigger_error('wrong params');
        return NULL;
    }
    $dlg_window = ncurses_newwin($dlg_rows, $dlg_cols, $dlg_y, $dlg_x);
    if (empty($dlg_window)) {
        return NULL;
    }
    ncurses_wborder($dlg_window, 0, 0, 0, 0, 0, 0, 0, 0);
    if ($title) {
        ncurses_wattron($dlg_window, NCURSES_A_REVERSE);
        ncurses_mvwaddstr($dlg_window, 0, 2, ' ' . $title . ' ');
        ncurses_wattroff($dlg_window, NCURSES_A_REVERSE);
    }
    ncurses_curs_set(1);
    // ncurses_wmove($dlg_window, 2, 2);
    ncurses_wrefresh($dlg_window);
    $do_getch = $clear;
    $input_val = '';
    $input_char = '';
    $input_len = 0;
    $cursor_x = 2;
    $cursor_y = 1;
    ncurses_wmove($dlg_window, $cursor_y, $cursor_x);
    ncurses_noecho();
    ncurses_keypad($dlg_window, TRUE);
    if ($valor) {
        ncurses_waddstr($dlg_window, $valor);
        $input_val = $valor;
        $input_len = strlen($valor);
        $cursor_x+=$input_len;
    }
    while ($do_getch) {
        $key_code = ncurses_wgetch($dlg_window);
        if ($key_code == NCURSES_KEY_LEFT) {
            ncurses_mvwaddstr($dlg_window, $cursor_y, 0, '   ');
            //ncurses_delwin($dlg_window);
            //continue;
            return -1;
            //break;
        }
        if (($key_code == XCURSES_KEY_CR) || ($key_code == XCURSES_KEY_LF)) {
            $do_getch = 0;
        } elseif ($key_code == NCURSES_KEY_BACKSPACE) {
            if ($input_len > 0) {
                $input_len--;
                $input_val = substr($input_val, 0, $input_len);
                $cursor_x--;
                ncurses_mvwaddstr($dlg_window, $cursor_y, $cursor_x, ' ');
                ncurses_wmove($dlg_window, $cursor_y, $cursor_x);
            }
        } elseif ($key_code < XCURSES_KEY_PRINTABLE_MIN || $key_code > XCURSES_KEY_PRINTABLE_MAX) {
            continue;
        } elseif ($input_len < $max_length && (($key_code >= 48 && $key_code <= 57) || ($title == "Reng" && ($key_code == 70 || $key_code == 102)) || ($title == "Ingrese Mesa" && ($key_code == 69 || $key_code == 101))

                )) {
            $input_val .= $input_char = chr($key_code);
            $input_len++;
            $cursor_x++;
            ncurses_waddstr($dlg_window, $input_char);
        }
    }
    ncurses_delwin($dlg_window);
    return $input_val;
}

function categorias_generales() {
    return array(1=>'Gobernador', 2=>'Diputado', 3=>'Senador', 4=>'Intendente', 5=>'Concejal',6=> 'Miembro Comunal');
}

function categorias_generales_i($i) {
    $temp=  categorias_generales();
    return $temp[$i];
}

?>